package gae.google.tcserver.db;

import java.util.List;

import gae.google.tcserver.db.factory.PMFactory;
import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.Key;

/**
 * The <tt>SaveToDB</tt> class saves information in the database
 *
 * @author Lars Friberg
 *
 * @date 2011-01-15
 */
public class SaveToDB {

	// creates an instance of PersistenceManager 
	private static PersistenceManager pm = PMFactory.get().getPersistenceManager();		

	/**
	 * save user 
	 * 
	 * @param user the user to save
	 */
	public synchronized static void saveUser(User user) {
		if(pm.isClosed()) {
			pm = PMFactory.get().getPersistenceManager();
		}
		try {
			if(user != null) {
				SaveToDB.pm.currentTransaction().begin();	// start a transaction
				pm.makePersistent(user);					// save user in database
				pm.detachCopy(user);						// detach user
				SaveToDB.pm.currentTransaction().commit();	// commit if no errors
			}
		}finally {
			if(SaveToDB.pm.currentTransaction().isActive()) { // roll back the transaction in case of an error
				SaveToDB.pm.currentTransaction().rollback();
			}
			if(!pm.isClosed()) {
				pm.close();
			}
		}
	}
	
	/**
	 * save team 
	 * 
	 * @param team the team to save
	 */
	public synchronized static void saveTeam(Team team) {
		if(pm.isClosed()) {
			pm = PMFactory.get().getPersistenceManager();
		}
		try {
			if(team != null) {
				SaveToDB.pm.currentTransaction().begin();	// start a transaction
				pm.makePersistent(team);					// save team in database
				pm.detachCopy(team);						// detach team
				SaveToDB.pm.currentTransaction().commit();	// commit if no errors
			}
		}finally {
			if(SaveToDB.pm.currentTransaction().isActive()) { // roll back the transaction in case of an error
				SaveToDB.pm.currentTransaction().rollback();
			}
			if(!pm.isClosed()) {
				pm.close();
			}
		}
	}

	/**
	 * save member entity key Id in the team entity
	 * 
	 * @param teamKeyId the poll question key id
	 * @param memberKeyId the member key id
	 */
	public synchronized static void saveTeamMemberKeyId(Key teamKeyId, Key memberKeyId) {
		if(pm.isClosed()) {
			pm = PMFactory.get().getPersistenceManager();
		}
		try {
			pm.currentTransaction().begin();			// start a transaction
			Team team = pm.getObjectById(Team.class, teamKeyId);
			List<Key> keys = team.getMemberKeyIds();
			keys.add(memberKeyId);						// add member key id to member key id's
			team.setMemberKeyIds(keys);
			pm.makePersistent(team);					// save the team to the database
			pm.detachCopy(team);						// detach the team
			pm.currentTransaction().commit();			// commit if no errors
		}finally {
			if (pm.currentTransaction().isActive()) {   // roll back the transaction in case of an error
		       pm.currentTransaction().rollback();
			}
			if(!pm.isClosed()) {
				pm.close();
			}
		}
	}
	
	/**
	 * save team member
	 * 
	 * @param teamMember the team member to save
	 */
	public synchronized static void saveTeamMember(TeamMember teamMember) {
		if(pm.isClosed()) {
			pm = PMFactory.get().getPersistenceManager();
		}
		try {
			if(teamMember != null) {
				SaveToDB.pm.currentTransaction().begin();	// start a transaction
				pm.makePersistent(teamMember);				// save team member in database
				pm.detachCopy(teamMember);					// detach team
				SaveToDB.pm.currentTransaction().commit();	// commit if no errors
			}
		}finally {
			if(SaveToDB.pm.currentTransaction().isActive()) { // roll back the transaction in case of an error
				SaveToDB.pm.currentTransaction().rollback();
			}
			if(!pm.isClosed()) {
				pm.close();
			}
		}
	}
	/**
	 * save activity 
	 * 
	 * @param activity the activity to save
	 */
	public synchronized static void saveActivity(Activity activity) {
		if(pm.isClosed()) {
			pm = PMFactory.get().getPersistenceManager();
		}
		try {
			if(activity != null) {
				SaveToDB.pm.currentTransaction().begin();	// start a transaction
				pm.makePersistent(activity);				// save activity in database
				pm.detachCopy(activity);					// detach activity
				SaveToDB.pm.currentTransaction().commit();	// commit if no errors
			}
		}finally {
			if(SaveToDB.pm.currentTransaction().isActive()) { // roll back the transaction in case of an error
				SaveToDB.pm.currentTransaction().rollback();
			}
			if(!pm.isClosed()) {
				pm.close();
			}
		}
	}
}
